﻿package com.wly.ui
{
	import com.wly.adapter.MapEditerMainAdapter;
	import com.wly.resource.RemoteResource;
	import com.wly.ui.common.IItemGrid;
	import com.wly.ui.common.SpriteObject;
	
	import fl.controls.UIScrollBar;
	import fl.events.ScrollEvent;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.filesystem.FileMode;
	import flash.filesystem.FileStream;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	public class ImgArea extends SpriteObject implements IItemGrid
	{
		private var _hightNum:int;
		private var _widthNum:int;
		private var _items:Array = [];
		private var _parentUI:MapEditerMain;
		private var _spMask:Sprite;
		private var _imgAreaScrollbar:UIScrollBar;
		private var _spContent:Sprite;
		private var _mcViewArea:Sprite;
		
		public function ImgArea()
		{
			super();
			
			_spMask = new Sprite();
			_mcViewArea = getChildByName("mcViewArea") as Sprite;
			this.mask = _mcViewArea;
			_imgAreaScrollbar = getChildByName("imgScrollbar") as UIScrollBar;
			_imgAreaScrollbar.addEventListener(ScrollEvent.SCROLL, onScroll);
		}
		
		private function onScroll(e:ScrollEvent):void
		{
			_spContent.y = -e.position;
		}
		
		public function set parentUI(ui:MapEditerMain):void
		{
			_parentUI = ui;
		}
		
		public function loadImg(url:String):void
		{
			var remoteResource:RemoteResource = RemoteResource.getInstance();
			remoteResource.requestRs(url, onLoadComplete);
		}
		
		public function onSelect(index:int):void
		{
			var item:ItemCell = _items[index] as ItemCell;
			MapEditerMainAdapter.CURBMD = item.getBmd();
		}
		
		public function setTop(displayObject:DisplayObject):void
		{
			if(displayObject && displayObject.parent==this)
			{
				setChildIndex(displayObject, numChildren-1);
			}
		}
		
		public function onItemMouseOver(index:int):void
		{
		}
		
		private function onLoadComplete(data:*):void
		{
			var bmp:Bitmap = data as Bitmap;
			if(bmp)
			{
				updateImgBmd(bmp.bitmapData);
			}
		}
		
		public function updateImgBmd(bmd:BitmapData):void
		{
			var i:int;
			var j:int;
			var item:ItemCell;
			var bmdCell:BitmapData;
			var rect:Rectangle = new Rectangle(0, 0, ItemCell.WIDTH, ItemCell.HIGHT);
			var pt:Point = new Point(0,0);
			
			if(bmd)
			{
				clearCells();
				_spContent = new Sprite;
				addChild(_spContent);
				_widthNum = (bmd.width-1)/ItemCell.WIDTH+1;
				_hightNum = (bmd.height-1)/ItemCell.HIGHT+1;
				
				for(i=0; i<_hightNum; i++)
				{
					for(j=0; j< _widthNum; j++)
					{
						rect.x = ItemCell.WIDTH*j;
						rect.y = ItemCell.HIGHT*i;
						
						bmdCell = new BitmapData(ItemCell.WIDTH, ItemCell.HIGHT);
						bmdCell.copyPixels(bmd, rect, pt);
						item = new ItemCell;
						item.parentUI = this;
						item.x = j*ItemCell.WIDTH;
						item.y = i*ItemCell.HIGHT;
						item.index = i*_widthNum+j;
						item.parentUI = this;
						item.updateBmd(bmdCell);
						_spContent.addChild(item);
						_items.push(item);
					}
				}
				_imgAreaScrollbar.setScrollProperties(480, 0, bmd.height-480);
				_imgAreaScrollbar.scrollPosition  = -_imgAreaScrollbar.minScrollPosition;
				_imgAreaScrollbar.update();
			}
		}
		
		private function clearCells():void
		{
			var i:int;
			var displayObj:DisplayObject;
			var item:ItemCell;
			
			if(_spContent)
			{
				for(i=0; i<_items.length; i++)
				{
					item = _items[i] as ItemCell;
					_spContent.removeChild(item);
				}
				_items = [];
			}
		}
	}
}